﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System.Device.Location;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;

namespace FeatureOverview.Features.Sensors
{
    public partial class Gps : PhoneApplicationPage
    {
        public Gps()
        {
            InitializeComponent();

            gcw = new System.Device.Location.GeoCoordinateWatcher();
            gcw.PositionChanged += (s, e) =>
                {
                    UpdatePosition(e.Position);
                };
            gcw.StatusChanged += (s, e) =>
                {
                    UpdateStatus(e.Status);
                };
            
        }

        private void UpdateStatus(System.Device.Location.GeoPositionStatus geoPositionStatus)
        {
            _status.Text = geoPositionStatus.ToString();   
        }

        private void UpdatePosition(System.Device.Location.GeoPosition<System.Device.Location.GeoCoordinate> geoPosition)
        {
            _latitude.Text = geoPosition.Location.Latitude.ToString();
            _longitude.Text = geoPosition.Location.Longitude.ToString();
            _altitude.Text = geoPosition.Location.Altitude.ToString();

            GeoCoordinate gc = 
                new GeoCoordinate(
                    geoPosition.Location.Latitude, 
                    geoPosition.Location.Longitude);
            _map.SetView(gc, 15.0);
        }
        
        System.Device.Location.GeoCoordinateWatcher gcw;
        bool mapmode = true;

        private void _on_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (Microsoft.Devices.Environment.DeviceType 
                == Microsoft.Devices.DeviceType.Emulator)
            {
                MessageBox.Show("Sorry. Not in the emulator");
                return;
            }
            gcw.Start();
        }

        private void _off_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            gcw.Stop();
        }

        private void _switchView_Click(object sender, RoutedEventArgs e)
        {
            if (mapmode)
            {
                AerialMode am = new AerialMode();
                _map.Mode = am;
                mapmode = false;
            }
            else
            {
                RoadMode rm = new RoadMode();
                _map.Mode = rm;
                mapmode = true;
            }
        }

    }
}